import apiClient from './axios';

// 用户组接口定义
export interface UserGroup {
  id: string;
  name: string;
  description?: string;
  permissions: string[];
  createdAt: string;
  updatedAt: string;
  memberCount?: number;
}

export interface UserGroupRequest {
  name: string;
  description?: string;
  permissions: string[];
}

export interface UserGroupResponse {
  success: boolean;
  message: string;
  data?: UserGroup;
}

export interface UserGroupListResponse {
  success: boolean;
  message: string;
  data?: {
    groups: UserGroup[];
    total: number;
  };
}

// 用户组API服务
const userGroupApi = {
  // 获取用户组列表
  getUserGroups: async (params?: { page?: number; pageSize?: number; name?: string }): Promise<UserGroupListResponse> => {
    try {
      const response = await apiClient.get('/user-groups', { params });
      return response.data;
    } catch (error) {
      console.error('获取用户组列表失败:', error);
      return {
        success: false,
        message: '获取用户组列表失败'
      };
    }
  },

  // 获取单个用户组详情
  getUserGroup: async (id: string): Promise<UserGroupResponse> => {
    try {
      const response = await apiClient.get(`/user-groups/${id}`);
      return response.data;
    } catch (error) {
      console.error('获取用户组详情失败:', error);
      return {
        success: false,
        message: '获取用户组详情失败'
      };
    }
  },

  // 创建用户组
  createUserGroup: async (groupData: UserGroupRequest): Promise<UserGroupResponse> => {
    try {
      const response = await apiClient.post('/user-groups', groupData);
      return response.data;
    } catch (error) {
      console.error('创建用户组失败:', error);
      return {
        success: false,
        message: '创建用户组失败'
      };
    }
  },

  // 更新用户组
  updateUserGroup: async (id: string, groupData: UserGroupRequest): Promise<UserGroupResponse> => {
    try {
      const response = await apiClient.put(`/user-groups/${id}`, groupData);
      return response.data;
    } catch (error) {
      console.error('更新用户组失败:', error);
      return {
        success: false,
        message: '更新用户组失败'
      };
    }
  },

  // 删除用户组
  deleteUserGroup: async (id: string): Promise<{ success: boolean; message: string }> => {
    try {
      const response = await apiClient.delete(`/user-groups/${id}`);
      return response.data;
    } catch (error) {
      console.error('删除用户组失败:', error);
      return {
        success: false,
        message: '删除用户组失败'
      };
    }
  },

  // 将用户添加到用户组
  addUserToGroup: async (groupId: string, userId: string): Promise<{ success: boolean; message: string }> => {
    try {
      const response = await apiClient.post(`/user-groups/${groupId}/users/${userId}`);
      return response.data;
    } catch (error) {
      console.error('将用户添加到用户组失败:', error);
      return {
        success: false,
        message: '将用户添加到用户组失败'
      };
    }
  },

  // 从用户组移除用户
  removeUserFromGroup: async (groupId: string, userId: string): Promise<{ success: boolean; message: string }> => {
    try {
      const response = await apiClient.delete(`/user-groups/${groupId}/users/${userId}`);
      return response.data;
    } catch (error) {
      console.error('从用户组移除用户失败:', error);
      return {
        success: false,
        message: '从用户组移除用户失败'
      };
    }
  },

  // 获取用户组成员列表
  getGroupMembers: async (groupId: string, params?: { page?: number; pageSize?: number }): Promise<any> => {
    try {
      const response = await apiClient.get(`/user-groups/${groupId}/users`, { params });
      return response.data;
    } catch (error) {
      console.error('获取用户组成员列表失败:', error);
      return {
        success: false,
        message: '获取用户组成员列表失败'
      };
    }
  }
};

export default userGroupApi; 